package com.cheems.service.impl;

import com.cheems.dao.UserMapper;
import com.cheems.entity.DTO.userDTO;
import com.cheems.entity.Pojo.user;
import com.cheems.exception.PasswordErrorException;
import com.cheems.exception.UserNotLoginException;
import com.cheems.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;

@Service
public class UserImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 用户登录
     * @param userDTO
     * @return
     */
    @Override
    public user login(userDTO userDTO) {
        String username = userDTO.getUsername();
        String password =  userDTO.getPassword();
        //根据用户名查询数据库中信息
        user user = userMapper.getByUsername(username);

        //处理异常信息(不存在该用户)
        if (user == null){
            //账号不存在
            throw new UserNotLoginException("用户未注册");
        }

        //密码比对
        if(!password.equals(user.getPassword())){
            //密码错误
            throw new PasswordErrorException("输入的密码错误");
        }

        return user;
    }

    /**
     * 用户注册
     * @param userDTO
     */
    @Override
    public void enroll(userDTO userDTO) {
        //非空处理
        String username = userDTO.getUsername();
        String password =  userDTO.getPassword();
        //处理异常信息(不存在该用户)
        if (username == null||password == null||username==""||password==""){
            //账号不存在
            throw new UserNotLoginException("请输入账号密码");
        }

        try {
            userMapper.enroll(userDTO);
        }catch (Exception e){
            throw new UserNotLoginException("用户名重复");
        }

    }
}
